﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Forms;
using System.IO;

namespace Visualization
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
		OpenFileDialog dataSrcDialog = new OpenFileDialog();
		List<FileInfo> sources = new List<FileInfo>();
        public MainWindow()
        {
            InitializeComponent();
			dataSrcDialog.Multiselect = true;
			dataSrcDialog.Filter = "XML Log Files|*.xml";
        }

        private void windowLoaded(object sender, System.Windows.RoutedEventArgs e)
        {
        	detectMonitors();
			updateVisualizationOptions();
        }
		
		private void detectMonitors(){

			//TO DO: add monitor detection code here
            if (Screen.AllScreens.Length == 1)
            {
                monitor1Text.Content = "monitor: detected";
                monitor2Text.Content = "monitor: not detected";
                monitor3Text.Content = "monitor: not detected";
                image.Source = new BitmapImage(new Uri(@"pack://application:,,,/Visualization;component/monitorEnabled.png"));
            }
            else if (Screen.AllScreens.Length == 2)
            {
                monitor1Text.Content = "monitor: detected";
                monitor2Text.Content = "monitor: detected";
                monitor3Text.Content = "monitor: not detected";
                image.Source = new BitmapImage(new Uri(@"pack://application:,,,/Visualization;component/monitorEnabled.png"));
                image2.Source = new BitmapImage(new Uri(@"pack://application:,,,/Visualization;component/monitorEnabled.png"));
            }
            else if (Screen.AllScreens.Length == 3)
            {
                monitor1Text.Content = "monitor: detected";
                monitor2Text.Content = "monitor: detected";
                monitor3Text.Content = "monitor: detected";
                image.Source = new BitmapImage(new Uri(@"pack://application:,,,/Visualization;component/monitorEnabled.png"));
                image2.Source = new BitmapImage(new Uri(@"pack://application:,,,/Visualization;component/monitorEnabled.png"));
                image3.Source = new BitmapImage(new Uri(@"pack://application:,,,/Visualization;component/monitorEnabled.png"));
            }
		}
		
		private void updateVisualizationOptions(){
            if (Screen.AllScreens.Length >= 2) {
                comboBox2.IsEnabled = true;
                if (Screen.AllScreens.Length >= 3)
                    comboBox3.IsEnabled = true;
            }
		}

		private void addDataSource(object sender, System.Windows.RoutedEventArgs e)
		{
			System.Windows.Forms.DialogResult result = dataSrcDialog.ShowDialog();
			if(result.Equals(System.Windows.Forms.DialogResult.OK)){
				foreach(string file in dataSrcDialog.FileNames){
					sources.Add(new FileInfo(file));
				}
				if(sources.Count > 1)
					fileSrcs.Content = "(multiple sources)";
				else
					fileSrcs.Content = sources[0].Name;
			}
		}

		private void startVisualization(object sender, System.Windows.RoutedEventArgs e)
		{
            if (sources.Count > 0)
            {
                warnings.Content = "";
                if (comboBox1.IsEnabled && !(comboBox1.SelectedIndex == 0))
                {
                    VisualizationCreator creator = new VisualizationCreator();
                    creator.bgWorker.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(bgWorker_ProgressChanged);
                    List<Object> args = new List<Object>();
                    if (comboBox1.SelectedIndex == 1)
                        args.Add(VisualizationCreator.VisualizationType.Density);
                    else if (comboBox1.SelectedIndex == 2)
                        args.Add(VisualizationCreator.VisualizationType.Disagreement);
                    else
                        args.Add(VisualizationCreator.VisualizationType.Trending);
                    args.Add(0);
                    args.Add(sources);
                    creator.bgWorker.RunWorkerAsync(args);
                }
                if (comboBox2.IsEnabled && !(comboBox2.SelectedIndex == 0))
                {
                    VisualizationCreator creator = new VisualizationCreator();
                    creator.bgWorker.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(bgWorker_ProgressChanged);
                    List<Object> args = new List<Object>();
                    if (comboBox2.SelectedIndex == 1)
                        args.Add(VisualizationCreator.VisualizationType.Density);
                    else if (comboBox2.SelectedIndex == 2)
                        args.Add(VisualizationCreator.VisualizationType.Disagreement);
                    else
                        args.Add(VisualizationCreator.VisualizationType.Trending);
                    args.Add(1);
                    args.Add(sources);
                    creator.bgWorker.RunWorkerAsync(args);
                }
                if (comboBox3.IsEnabled && !(comboBox3.SelectedIndex == 0))
                {
                    VisualizationCreator creator = new VisualizationCreator();
                    creator.bgWorker.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(bgWorker_ProgressChanged);
                    List<Object> args = new List<Object>();
                    if (comboBox3.SelectedIndex == 1)
                        args.Add(VisualizationCreator.VisualizationType.Density);
                    else if (comboBox3.SelectedIndex == 2)
                        args.Add(VisualizationCreator.VisualizationType.Disagreement);
                    else
                        args.Add(VisualizationCreator.VisualizationType.Trending);
                    args.Add(2);
                    args.Add(sources);
                    creator.bgWorker.RunWorkerAsync(args);
                }
            }
            else
                warnings.Content = "warning: no data sources selected";
			//creator.bgWorker.RunWorkerAsync(args);
		}

        void bgWorker_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
        {
            warnings.Content = "Progress: " + e.UserState;
            progress.Value = e.ProgressPercentage;
        }

		private void convertCSV(object sender, System.Windows.RoutedEventArgs e)
		{
			Visualization.DataModel.CSVProcessingGUI converter = new Visualization.DataModel.CSVProcessingGUI();
			converter.Show();
		}
    }
}
